package com.xiaoyu.lanling.event.user

import `in`.srain.cube.request.JsonData
import com.xiaoyu.base.data.UserInfoDataProvider
import com.xiaoyu.base.event.BaseJsonEvent
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.JsonUtil
import com.xiaoyu.base.utils.ListUtil
import com.xiaoyu.base.utils.extensions.dp
import com.xiaoyu.lanling.feature.user.model.FeedImageItem
import com.xiaoyu.lanling.feature.user.model.GiftWallItem
import com.xiaoyu.lanling.feature.user.model.UserTag
import com.xiaoyu.lanling.feature.vip.model.VipInfo
import com.xiaoyu.lanling.media.image.ImageLoadParam

class UserHomePageEvent(requestTag: Any, jsonData: JsonData) : BaseJsonEvent(requestTag, jsonData) {
    val userJsonData = jsonData.optJson("user");
    val user: User = User.fromJson(userJsonData)
    val verify: String = userJsonData.optString("verify")
    val age: Int = userJsonData.optInt("age")
    val subscribe: Boolean = jsonData.optBoolean("subscribe")
    val black: Boolean = jsonData.optBoolean("black")
    val soliloquy: String = jsonData.optString("soliloquy")
    val distance: String = jsonData.optString("distance")
    val vipInfo = VipInfo(jsonData.optJson("vipInfo"))
    val photos: MutableList<String> = jsonData.optJson("photos").asList<String>()
    val tags: MutableList<UserTag>? = ListUtil.convertToList(jsonData.optJson("tags").asList<String>()) { tag -> UserTag(tag) }
    val feedImages: MutableList<FeedImageItem> = ListUtil.convertToList(jsonData.optJson("feedImages").asList<String>()) { FeedImageItem(it) }
    val feedCount = jsonData.optString("feedCount")
    val charmingInfo = jsonData.optJson("charmInfo")
    val charmingIcon = charmingInfo.optString("icon")
    val charmingNum = charmingInfo.optString("value")
    val generosityInfo = jsonData.optJson("generosityInfo")
    val generosityNum = generosityInfo.optString("value")
    val generosityIcon = generosityInfo.optString("icon")
    val guardInfo = GuardInfo(jsonData.optJson("guardInfo"))
    val giftWallList = JsonUtil.convertToList(jsonData.optJson("giftWall")) { itemData -> GiftWallItem(itemData) }
    val generosityIconLoadParam = ImageLoadParam.newBuilder()
            .setUrl(generosityIcon)
            .setWidth(80.dp.toInt())
            .setHeight(24.dp.toInt())
            .setEnableEmptyUrl(true)
            .build()
    val charmingIconLoadParam = ImageLoadParam.newBuilder()
            .setUrl(charmingIcon)
            .setWidth(80.dp.toInt())
            .setHeight(24.dp.toInt())
            .setEnableEmptyUrl(true)
            .build()

    init {
        UserInfoDataProvider.getInstance().updateUser(user)
    }

    inner class GuardInfo(jsonData: JsonData) {
        val user = User.fromJson(jsonData.optJson("user"))
        val levelIcon = jsonData.optString("levelIcon")
        val needCoin = jsonData.optInt("needCoin")
    }
}